import User from "../../../models/user";
import {handlerGobackClick, hint} from "../../../utils/util";

const user = new User()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: "",
        password: ""
    },
    handlerGobackClick,
    authCodeLogin(){
        wx.redirectTo({
            url:"/owner-app/pages/auth-code-login/index"
        })
    },
    goRegister(){
        wx.redirectTo({
            url:"/owner-app/pages/register/index"
        })
    },
    goBefore(){
        wx.redirectTo({
            url:"/pages/login-before/index"
        })
    },
    pwLogin() {
        if(!this.data.phone){
            hint("请输入手机号")
            return;
        }
        if(!this.data.password){
            hint("请输入密码")
            return;
        }
        let {phone, password} = this.data
        user.userLogin(phone, password)
            .then((res) => {
                let status = res.result.registerStatus
                if(status === 0){
                    hint(res.result.message)
                    return
                }
                if(status === 1){
                    wx.setStorageSync("token",res.messageBody.token)
                    wx.setStorageSync("user",res.messageBody.user)
                    let type = res.messageBody.user.type
                    if(type === '业主'){
                        wx.switchTab({
                            url:"/pages/home/index"
                        })
                        
                        return;

                    }
                    if (type === '访客'){
                        wx.reLaunch({
                            url:"/pages/home/index"
                        })
                        return;
                    }
                }
                hint(res.result.message)

            })
            .catch((err) => {
                console.log(err);
            })
    },
    inputPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    inputPassword(e) {
        this.setData({
            password: e.detail.value
        })
    }
})
